/**
 *Ajax utility class
 *@author Lizhen
 *@depend prototype.js
 */


//Constants
var CALL_NOTHING="[001]";
var CALL_FUNCTION ="[002]";
var CALL_SUCCESS ="[003]";
var CALL_PAGECODE ="[004]";


/**
 *@param url Forward url
 *@param params The parameters
 *@param fTarget  Target window
 *@param sFeatures Window paramters
 *@param doBeforeCallPage A js function ,do it before callpage
 *@param doAfterCallPage A js function ,do it after callpage
 */
function AjaxSubmit(url,params,fTarget,sFeatures,doBeforeCallPage,doAfterCallPage){
/**
 *process logic after submit complete.
 */
var onCompleteEvent=function(request){
  if(ajax_getRequestType(request)==CALL_PAGECODE){
     if(typeof(doBeforeCallPage)=="function"){
          doBeforeCallPage();
     }
  
      var _fTarget = "";
     if(typeof(fTarget)!="undefined")_fTarget = fTarget;
     
     if(_fTarget=="_self" || _fTarget=="" || _fTarget==null)_fTarget="_blank";
     
     var _sFeatures = "";
     if(typeof(sFeatures)!="undefined")_sFeatures = sFeatures
     
     var win = window.open("about:blank");
     var ss = 'a'+request.responseText.substring(6,11)+'a';
     win.document.write(ss);    
     win.location.reload(false); //refresh page.  
     
     if(typeof(doAfterCallPage)=="function"){
          doAfterCallPage();
     }
     
  }else if(ajax_getRequestType(request)==CALL_NOTHING){
     window.setTimeout("dostart()",1000); 
  }else if(ajax_getRequestType(request)==CALL_FUNCTION){
     eval(ajax_getRequestContent(request)); 
     new AjaxSubmit("/bluestone/ajax.do","command=success&jsid="+ajax_getRequestID(request),null,null);      
  }else if(ajax_getRequestType(request)==CALL_SUCCESS){
     window.setTimeout("dostart()",1000);   
  }
};


var ajaxSubmit = function(url,params){
  new Ajax.Request(url, 
  { 
  method: 'post',
  parameters: params,
  onComplete: onCompleteEvent,
  onFailure:  ajax_errorEv
  }); 
};

 ajaxSubmit(url,params);//do submit;


}


/**
 *@param formName Form name
 *@param fTarget  Target window
 *@param sFeatures Window paramters
 *@param doBeforeCallPage A js function ,do it before callpage
 *@param doAfterCallPage A js function ,do it after callpage
 */
function AjaxSubmitForm(formName,fTarget,sFeatures,doBeforeCallPage,doAfterCallPage){

var formEl = document.getElementsByName(formName)[0];



/**
 *process logic after submit complete.
 */
var onCompleteEvent=function(request){
  if(ajax_getRequestType(request)==CALL_PAGECODE){
     if(typeof(doBeforeCallPage)=="function"){
          doBeforeCallPage();
     }
  
      var _fTarget = formEl.target;
     if(typeof(fTarget)!="undefined")_fTarget = fTarget;
     
     if(_fTarget=="_self" || _fTarget=="" || _fTarget==null)_fTarget="_blank";
     
     var _sFeatures = "";
     if(typeof(sFeatures)!="undefined")_sFeatures = sFeatures
     alert(request.responseText);
     
     //var win = window.open("about:blank",_fTarget,_sFeatures);
     //win.document.write(request.responseText);  
     //win.title = request.responseText;  
     //win.location.reload(false); //refresh page.  
     
     if(typeof(doAfterCallPage)=="function"){
          doAfterCallPage();
     }
     
  }else if(ajax_getRequestType(request)==CALL_NOTHING){
     alert(ajax_getRequestContent(request));
  }else if(ajax_getRequestType(request)==CALL_FUNCTION){
     eval(ajax_getRequestContent(request));    
  }else if(ajax_getRequestType(request)==CALL_SUCCESS){
     //TODO:add logic for redirect.   
  }
};


var submitForm = function(formEl){
  var f = formEl;
  var url = f.action;
  new Ajax.Request(url, 
  { 
  method: 'post',
  parameters: Form.serialize(f),
  onComplete: onCompleteEvent,
  onFailure:  ajax_errorEv
  }); 
};

 submitForm(formEl);//do submit;
}



var ajax_getRequestType = function(request){
  var prex = request.responseText.substring(6,11);
  if(prex==CALL_NOTHING)        {return CALL_NOTHING;}
  else if(prex==CALL_FUNCTION){ return CALL_FUNCTION;}
  else if(prex==CALL_SUCCESS){ return CALL_SUCCESS;}
  else return CALL_PAGECODE;//default return page.
};

var ajax_getRequestID = function(request){
  var t = request.responseText;
  var c = t.substring(5,18);
  return c;
};

var ajax_getRequestContent = function(request){
   var t = request.responseText;
   var c = t.substring(18,t.length);
   return c;
};


var ajax_errorEv=function(t){alert('Error ' + t.status + ' -- ' + t.statusText);};

if(window.attachEvent)window.attachEvent("onload",dostart);
else if(window.addEventListener)window.addEventListener("load",dostart,false);


function dostart(){
   new AjaxSubmit("/bluestone/ajax.do","command=query",null,null)
}

